﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using log4net;
using NHibernate.Criterion;
using BidFunLib.Entity;


namespace BidFunLib.DAO
{
    class BidUserDAO 
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(BidUserDAO));

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pseudo"></param>
        /// <returns></returns>
        public BidUser getBidUserByPseudo(String pseudo)
        {
            BidUser bidUser = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();


                IList<BidUser> users  =  session.CreateCriteria(typeof(BidUser)).Add(Expression.Like("Pseudo", pseudo)).List<BidUser>();

                if (users != null && users.Count > 0)
                {
                    bidUser = users.First();
                }
                transaction.Commit();
                session.Close();
                return bidUser;
            }
        }
		
		
        public BidUser getBidUserByPseudo2(String pseudo)
        {
            BidUser bidUser = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
				IList<BidUser> users = (IList<BidUser>) session.CreateQuery("from BidUser bu where bu.Pseudo= ?")
				.SetString(0, pseudo)
				.List<BidUser>();
				
                if (users != null && users.Count > 0)
                {
                    bidUser = users.First();
                }
                transaction.Commit();
                session.Close();
                return bidUser;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        public void saveBidUser(BidUser bidUser)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                log.Debug("Saving newBidUser " + bidUser.Pseudo);
                Int32 newID = (Int32)session.Save(bidUser);
                log.Debug("newBidUser " + bidUser.Pseudo + " saved !");
                transaction.Commit();
                session.Close();
            }
        }

        public BidUser getUserById(Int32 id)
        {
            BidUser biduser = null;
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                IList<BidUser> objects = (IList<BidUser>)session.CreateQuery("from BidUser bo where bo.IdBidUser= ?")
                .SetString(0, id.ToString())
                .List<BidUser>();

                if (objects != null && objects.Count > 0)
                {
                    biduser = objects.First();
                }
                transaction.Commit();
                session.Close();
                return biduser;
            }
        }
    }
}
